﻿using System;
using System.Windows.Forms;
using Domain.Entity;
using QQWBDao;
using Spider.Service;
using Timer = System.Timers.Timer;

namespace QQWBService
{
    public class QQWBVisiteService
    {
        /// <summary>
        /// 启动QQ微博访问
        /// </summary>
        /// <param name="browser"></param>
        /// <returns></returns>
        public static string StartToQQWbVisite(WebBrowser browser)
        {
            SpiderService.ReSetSpiderConfig();
            AntService.CommandAntCaptainToWork();
            SpiderService.CommandSpiderToWorkBySTA(browser);
            return "";
        }
        /// <summary>
        /// 自动定时添加待访问的QQ号码
        /// </summary>
        /// <param name="count"></param>
        public static void AutoAddWaitVisitedQQNumberForWB(int count)
        {
            var taskcount = SpiderMonitor.GetTodoTaskCount();
            if (taskcount == 0)
            {
                var qqsemails = QqEmailDao.GetHasNoVisiteQQZoneUserEmailForWB(count);
                string[] qqs = new string[qqsemails.Length];
                for (int i = 0; i < qqsemails.Length; i++)
                {
                    qqs[i] =qqsemails[i].Replace("@qq.com", "");
                    QqEmailDao.SetQQWbHasVisitedForWB(qqsemails[i]);
                }
                SpiderService.AddContentToTodoQueue(qqs, "qqforwb");
            }
        }

        public static void ReSetAllQQAcountWbMaxVisiteTimesForWB(int times)
        {
            QQAcountDao.SetAllQQAcountMaxVisiteTimesForWB(times);
        }
        public static int GetCurrentMaxVisiteTimesForWB()
        {
            var first =CommonDao.QQAcountDao.GetFirstAcount();
            if (first != null)
            {
                return first.MaxVisiteTimesForWB;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 定时更新QQ账号已经访问次数，方便自动每日定时访问
        /// </summary>
        public static void AutoUpdateQQAcountHasVisiteTimesForWb()
        {
            Timer t = new Timer();
            t.Interval = 1000 * 60 * 30;//半小时执行一次
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(
            delegate
            {
                var qqAcounts =CommonDao. QQAcountDao.GetAllAcount();
                foreach (QQAcount qqAcount in qqAcounts)
                {
                    if (qqAcount.LastLoginForWB != null)
                    {
                        if (DateTime.Now - qqAcount.LastLoginForWB > new TimeSpan(0, 24, 0, 0))
                        {
                            QQAcountDao.SetQQAcountHasVisiteTimesForWB(0, qqAcount.QQ);
                        }
                    }
                    else
                    {
                        qqAcount.LastLoginForWB = DateTime.Now;
                        QQAcountDao.SetQQAcountHasVisiteTimesForWB(0, qqAcount.QQ);
                    }
                }
            });

        }

        public static void AddURLtoTaskQueen(string url)
        {
            SpiderService.AddUrlToTodoQueue(new string[] {url});
        }
    }
}
